//
//  HNSH_TaskCenterViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/29.
//

#import "HNSH_TaskCenterViewController.h"

#import "HNSH_TaskCenterCell.h"

#import "ST_PayPasswordView.h"           //支付弹窗

#import "HNSH_ExchangeNetServer.h"

@interface HNSH_TaskCenterViewController ()

/**  */
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

@end

static NSString *HNSH_TaskCenterCellID = @"HNSH_TaskCenterCellID";

@implementation HNSH_TaskCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configTableView];
}

- (void)configTableView {
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.tableView.rowHeight = 132;
    self.tableView.estimatedRowHeight = 132;
    [self.tableView registerClass:[HNSH_TaskCenterCell class] forCellReuseIdentifier:HNSH_TaskCenterCellID];
    
    [self setupRefresh:self.tableView option:ST_RefreshNone];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@"暂无数据"];
}

- (void)setType:(NSInteger)type {
    _type = type;
    [self headerRefreshing];
}

- (void)refreshData:(NSInteger)page {
    if (self.type == 0) {
        [HNSH_ExchangeNetServer requestQuestConfigsSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
            [self endRefresh:YES];
            if (responseObject.code == 0) {
                if (page == 1) {
                    [self.listAry removeAllObjects];
                }
                NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_TaskCenterListModel class] json:responseObject.resultset];
                [self.listAry addObjectsFromArray:ary];
                [self.tableView reloadData];
                [self endRefresh:ary.count >= 10];
            }
        } Failure:^(NSError * _Nullable error) {
            
        }];
    } else {
        [HNSH_ExchangeNetServer requestQuestReceiveListWithPage:page type:self.type success:^(ST_BaseNetModel * _Nullable responseObject) {
            [self endRefresh:YES];
            if (responseObject.code == 0) {
                if (page == 1) {
                    [self.listAry removeAllObjects];
                }
                NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_TaskCenterMyListModel class] json:responseObject.resultset[@"list"]];
                [self.listAry addObjectsFromArray:ary];
                [self.tableView reloadData];
                [self endRefresh:ary.count >= 10];
            }
        } Failure:^(NSError * _Nullable error) {
            
        }];
    }
    
}

- (void)taskBuyWithCellModel:(HNSH_TaskCenterListModel *)cellModel password:(NSString *)password type:(NSInteger)type {
    [HNSH_ExchangeNetServer requestQuestReceiveWithQuestId:cellModel.uid type:type quantity:@"1" pay_password:password success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"兑换成功"];
            [self headerRefreshing];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark action
//兑换
- (void)cellButtonClickWithCellModel:(HNSH_TaskCenterListModel *)cellModel {
    if (cellModel.surplus_count_extra > 0) {//有免费可领取
        [self payMoneyWithCellModel:cellModel type:1];
        return;
    }
    if (cellModel.surplus_count > 0) {//可以购买
        [self payMoneyWithCellModel:cellModel type:0];
    } else {
        [self.view makeToast:@"无兑换次数"];
    }
}

//MARK: 支付
- (void)payMoneyWithCellModel:(HNSH_TaskCenterListModel *)cellModel type:(NSInteger)type {
    [ST_PayPasswordView showPasswordViewWithSureBlock:^(NSString * _Nonnull password) {
        [self taskBuyWithCellModel:cellModel password:password type:type];
    } cancelBlock:^{
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_TaskCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_TaskCenterCellID];
    if (self.type == 0) {
        cell.model = self.listAry[indexPath.row];
    } else {
        cell.myModel = self.listAry[indexPath.row];
    }
    
    WeakSelf(weakSelf);
    cell.cellButtonBlock = ^(HNSH_TaskCenterListModel * _Nonnull selectModel) {
        [weakSelf cellButtonClickWithCellModel:selectModel];
    };
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    !self.scrollCallback ?: self.scrollCallback(scrollView);
}

//MARK: JXPagingViewListViewDelegate Methods
- (UIView *)listView{
    return self.view;
}

- (UIScrollView *)listScrollView{
    return self.tableView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback{
    self.scrollCallback = callback;
}

- (void)listWillAppear{
    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

- (void)listDidAppear{
    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

- (void)listWillDisappear{
    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

- (void)listDidDisappear{
    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
